<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="98px"
    >
      <el-form-item label="用户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="培训车型" prop="carType">
        <el-input
          v-model="queryParams.carType"
          placeholder="请输入培训车型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button @click="sendSms"><Icon icon="ep:refresh" class="mr-5px" /> 群发短信</el-button>
        <el-button @click="sendMp"
          ><Icon icon="ep:refresh" class="mr-5px" /> 群发模板消息</el-button
        >
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['scrm:enroll-user:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['scrm:enroll-user:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
      ref="tableRef"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="培训车型" align="center" prop="carType" />
      <el-table-column
        label="报名时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
      />
      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-hasPermi="['train:student:update']"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            v-hasPermi="['train:student:delete']"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StudentForm ref="formRef" @success="getList" />

  <!-- 群发短信弹窗 -->
  <send-sms-form @sended="sended" ref="smsFormRef" />
  <!-- 群发公众号模板消息弹窗 -->
  <send-mp-template-form @sended="sended" ref="mpFormRef" />
</template>

<script setup lang="ts" name="ScrmStudent">
import { dateFormatter } from '@/utils/formatTime'
import * as StudentApi from '@/api/scrm/student/index'
import { SendSmsForm } from '@/components/SendSmsForm'
import { SendMpTemplateForm } from '@/components/SendMpTemplateForm'
import StudentForm from './student-form.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const multipleSelection = ref<StudentApi.StudentVO[]>([]) // 表格的选择项
const sendPhoneList = ref<string[]>([]) // 发送短信的手机号列表
const openidList = ref<string[]>([]) // 发送模板消息的openid列表
const tableRef = ref() // 表格的ref
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: null,
  avatar: null,
  name: null,
  mobile: null,
  createTime: null,
  cartype: null,
  comment: null,
  gender: null,
  openid: ''
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StudentApi.getStudentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: string) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StudentApi.deleteStudent(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  // try {
  //   // 导出的二次确认
  //   await message.exportConfirm()
  //   // 发起导出
  //   exportLoading.value = true
  //   const data = await UserApi.exportUser(queryParams)
  //   download.excel(data, '用户.xls')
  // } catch {
  // } finally {
  //   exportLoading.value = false
  // }
}

/** 表格的选择项 */
const handleSelectionChange = (val: StudentApi.StudentVO[]) => {
  multipleSelection.value = val
  sendPhoneList.value = val.map((item) => item.mobile)
  openidList.value = val.map((item) => item.openid)
}

/** 群发短信 */
const smsFormRef = ref()
const sendSms = () => {
  let type: 'check' | 'uncheck' = 'uncheck' // 是否勾选了用户
  let params = queryParams
  let num = total.value * queryParams.pageSize
  if (multipleSelection.value.length > 0) {
    type = 'check'
    params = sendPhoneList.value
    num = multipleSelection.value.length
  }
  smsFormRef.value.open(type, params, num)
}

/** 群发微信公众号模板消息 */
const mpFormRef = ref()
const sendMp = () => {
  let type: 'check' | 'uncheck' = 'uncheck' // 是否勾选了用户
  let params = queryParams
  let num = total.value * queryParams.pageSize
  if (multipleSelection.value.length > 0) {
    type = 'check'
    params = sendPhoneList.value
    num = multipleSelection.value.length
  }
  mpFormRef.value.open(type, params, num)
}

// 信息发送成功回调
const sended = () => {
  tableRef.value!.clearSelection()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
